package com.syqc.monitor.ui.alarm

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import autodispose2.autoDispose
import com.syqc.comlib.auto.AutoDisposeViewModel
import com.syqc.comlib.utils.AppContext
import com.syqc.comlib.utils.GsonUtil
import com.syqc.entity.AlarmDetail
import com.syqc.entity.AlarmHandle
import com.syqc.entity.AlarmMedia
import com.syqc.entity.AlarmNote
import com.syqc.monitor.R
import com.syqc.net.ServiceHelper
import com.syqc.utils.TimeUtil

class HandleAlarmVM : AutoDisposeViewModel() {
    private val _alarms = MutableLiveData<ArrayList<AlarmDetail>>()
    val alarms: LiveData<ArrayList<AlarmDetail>> = _alarms

    private val _handle = MutableLiveData<Boolean>()
    val handle: LiveData<Boolean> = _handle

    private val allAlarms = ArrayList<AlarmDetail>()
    private val _handleAll = MutableLiveData<Boolean>()
    val handleAll: LiveData<Boolean> = _handleAll

    private val _start = MutableLiveData<ArrayList<AlarmMedia>>()
    val start: LiveData<ArrayList<AlarmMedia>> = _start

    var plate: String = ""
    var carId: String = ""


    var isCar = false

    //搜索的key
    private var upKey: String = ""


    fun getAlarmByType(alarmType: String) {
        toastLoading(true)
        allAlarms.clear()
        ServiceHelper.getRepository().getAlarmDetail(alarmType).autoDispose(this)
            .subscribe({
                toastLoading(false)
                if (it.isNullOrEmpty()) {
                    toast(R.string.desc_no_about_data)
                    _alarms.postValue(it)
                    return@subscribe
                }
                allAlarms.addAll(it)
                _alarms.postValue(it)
            }, error)
    }

    private val searchAlarms = ArrayList<AlarmDetail>()

    fun search(key: String) {
        upKey = key.uppercase()
        searchAlarms.clear()
        if (isCar) {
            allAlarms.forEach {
                if (it.alarmName.uppercase().contains(upKey)) searchAlarms.add(it)
            }
        } else {
            allAlarms.forEach {
                if (it.carPlate.uppercase().contains(upKey) || it.carName.uppercase()
                        .contains(upKey)
                ) searchAlarms.add(it)
            }
        }
        _alarms.postValue(searchAlarms)
    }

    //全选和不全选
    fun chooseAll(isChoose: Boolean) {
        allAlarms.forEach {
            it.isChoose = isChoose
        }
        search(upKey)
    }

    fun isAllChoose(): Boolean {
        allAlarms.forEach {
            if (!it.isChoose) return false
        }
        return true
    }

    fun isNoChoose(): Boolean {
        allAlarms.forEach {
            if (it.isChoose) return false
        }
        return true
    }

    private val chooseList = ArrayList<AlarmDetail>()
    fun hasChoose(): Boolean {
        val alarms = allAlarms.filter { it.isChoose }
        chooseList.addAll(alarms)

        if (alarms.isEmpty()) {
            if (isCar) {
                toast(R.string.hint_choose_alarm_type)
            } else {
                toast(R.string.hint_choose_alarm_type_car)
            }
            return false
        }
        return true
    }

    /**
     * 处理所有报警
     *
     */
    fun handleAll(solveDes: String) {
        toastLoading(true)
        val list = ArrayList<AlarmHandle>()
        chooseList.forEach {
            list.add(AlarmHandle(it.time, it.carId, it.alarmType, it.src))
        }
        val json = GsonUtil.toJson(list)
        ServiceHelper.getRepository().handleAlarms(json, solveDes).autoDispose(this).subscribe({
            toastLoading(false)
            if (it.status == 1) {
                toast(R.string.taost_handle_success)
                _handleAll.postValue(true)
            } else {
                _handleAll.postValue(false)
                toast(it.result.toString())
            }
        }, {
            toast(it.message ?: "")
            toastLoading(false)
            _handleAll.postValue(false)
        })
    }


    /**
     * Process alarm
     *
     * @param carId
     * @param alarmType
     * @param time
     * @param solveDes
     */
    fun processAlarm(carId: String, alarmType: String, time: String, solveDes: String) {
        toastLoading(true)
        ServiceHelper.getRepository()
            .processAlarm(carId, alarmType, time, solveDes).autoDispose(this)
            .subscribe({
                toastLoading(false)
                if (it.status == 0) {
                    _handle.postValue(false)
                    toast(it.result.toString())
                } else {
                    _handle.postValue(true)
                    toast(R.string.taost_handle_success)
                }
            }, {
                toast(it.message ?: "")
                toastLoading(false)
                _handle.postValue(false)
            })
    }

    fun getAlarmInfo(startTime: String, alarmType: String) {
        toastLoading(true)
        ServiceHelper.getRepository().getAlarmMedia(
            carId,
            TimeUtil.instance.formatHm(startTime),
            alarmType
        ).autoDispose(this).subscribe({
            toastLoading(false)
            if (it.isNullOrEmpty()) {
                toast(R.string.desc_no_about_data)
                return@subscribe
            }
            _start.postValue(it)

        }, error)
    }


    fun getAlarmDetailCar(carId: String) {
        allAlarms.clear()
        toastLoading(true)
        ServiceHelper.getRepository().getAlarmDetailCar(carId).autoDispose(this)
            .subscribe({
                toastLoading(false)
                if (it.isNullOrEmpty()) {
                    toast(R.string.desc_no_about_data)
                    _alarms.postValue(it)
                    return@subscribe
                }
                allAlarms.addAll(it)
                _alarms.postValue(it)
            }, error)
    }

    var mAlarmNote: AlarmNote? = null
    var isNote404 = false
    val noteLive = MutableLiveData<AlarmNote>()

    /**
     * 获取文本短语
     */
    fun getAlarmNote() {
        if (mAlarmNote != null) return
        toastLoading(true)
        ServiceHelper.getRepository().getAlarmNote().autoDispose(this).subscribe({
            toastLoading(false)
            isNote404 = false
            if (it.sysNote.isNullOrEmpty() && it.userNote.isNullOrEmpty()) {
                mAlarmNote = null
            }
            mAlarmNote = it
            noteLive.postValue(mAlarmNote)
        }, {
            toastLoading(false)
            it.message?.run { toast(this) }
            isNote404 = it.message == AppContext.getDescById(R.string.net_error_404)
        })
    }

}